package OrderService

import (
	"context"
	"errors"
	"fmt"
	"gdshop-back-end-go/client-api/app/request/OrderReq"
	"gdshop-back-end-go/client-api/app/response/OrderResp"
	toolsDb "gdshop-back-end-go/common/db"
	"gdshop-back-end-go/common/response/GoodsResp"
	"gdshop-back-end-go/service-other/GoodsService"
	"github.com/gogf/gf/util/gconv"
	"github.com/gogf/gf/util/gvalid"
	"time"
)

// 检测一条商品
func DoCreateOne(ctx context.Context, goods *OrderReq.GoodsItem, memberId int) (*OrderResp.Goodss, error) {
	// 判断基本参数
	rules := map[string]string{
		"GoodsId":       "required|integer|min:1",
		"GoodsOptionId": "required|integer|min:1",
		"GoodsNum":      "required|integer|min:1",
	}
	msgs := map[string]interface{}{
		"GoodsId": map[string]string{
			"required": "请选择商品",
			"integer":  "商品ID格式错误",
			"min":      "商品ID格式错误",
		},
		"GoodsOptionId": map[string]string{
			"required": "请选择商品规格",
			"integer":  "商品规格ID格式错误",
			"min":      "商品规格ID格式错误",
		},
		"GoodsNum": map[string]string{
			"required": "请填写商品数量",
			"integer":  "商品数量格式错误",
			"min":      "数量不能小于1",
		},
	}

	if e := gvalid.CheckStruct(ctx, goods, rules, msgs); e != nil {
		return nil, e
	}

	// 先找出是否有当前商品
	goodsModel, err := GoodsService.GetGoodsModel(ctx, goods.GoodsId)
	if err != nil {
		return nil, err
	}

	// 判断是否是上架状态
	if goodsModel.Status != 1 {
		return nil, errors.New("商品《" + goodsModel.GoodsName + "》已下架")
	}

	var optionModel *GoodsResp.GoodsOption

	for _, option := range goodsModel.Options {
		if option.Id == goods.GoodsOptionId {
			optionModel = option
		}
	}
	if optionModel == nil {
		return nil, errors.New("找不到规格")
	}

	// 判断团购 是否选择了团购
	if goods.TuanMode > 0 {
		fmt.Println("---------- 团购模式 ----------")

		if goods.TuanId < 1 {
			return nil, errors.New("请选择团购")
		}
		// 团购后续判断
		if goodsModel.TuanInfo.TuanId != goods.TuanId {
			return nil, errors.New("团购信息错误")
		}

		// 判断团购是否过期
		if goodsModel.TuanInfo.TuanEndTime < time.Now().Unix() {
			return nil, errors.New("团购已结束")
		}
		// 判断团购是否有读取到规格数据
		if len(goodsModel.TuanOptions) < 1 {
			return nil, errors.New("团购规格错误")
		}
		var tuanOptionModel *GoodsResp.GoodsOption
		// 判断团购规格是否存在
		for _, item := range goodsModel.TuanOptions {
			if item.Id == goods.GoodsOptionId {
				tuanOptionModel = item
				break
			}
		}
		if tuanOptionModel == nil {
			return nil, errors.New("您选择团购规格不存在")
		}
		// 判断团购规格是否有库存
		if tuanOptionModel.Stock < goods.GoodsNum {
			return nil, errors.New("您选择团购规格库存不足")
		}
		// 判断如果是参团，查询团购是否存在
		if goods.TuanMode == 2 {
			// 查询参团是否存在
			tmpMemberId, err := toolsDb.GetUnSafaTable(ctx, "order_tuan").Where("tuan_join_id", goods.TuanJoinId).Value("member_id")
			if err != nil {
				return nil, err
			}
			if tmpMemberId.IsEmpty() {
				return nil, errors.New("参团信息不存在")
			}
			// 不能参加自己的团，二期加入一个开关来控制
			if tmpMemberId.Int() == memberId {
				return nil, errors.New("不能参加自己的团")
			}
		} else if goods.TuanMode == 1 {
			// 开团
			// 判断完毕
		}
		// 只保留一个规格
		goodsModel.TuanOptions = []*GoodsResp.GoodsOption{
			tuanOptionModel,
		}
	} else {
		// 普通模式，删除掉团购规格
		goodsModel.TuanOptions = nil
		// 数量是否超库存
		if goods.GoodsNum > optionModel.Stock {
			return nil, errors.New(optionModel.Title + "规格库存不足")
		}
	}

	// 只保留一个规格
	goodsModel.Options = []*GoodsResp.GoodsOption{
		optionModel,
	}

	var goodssItem *OrderResp.Goodss
	if err := gconv.Struct(goodsModel, &goodssItem); err != nil {
		return nil, err
	}

	goodssItem.BuyNum = goods.GoodsNum

	goodssItem.DiscountMoney = 0
	if goods.TuanMode > 0 {
		goodssItem.Paid = goodsModel.TuanOptions[0].SellPrice * int64(goods.GoodsNum)
		goodssItem.SellPrice = goodsModel.TuanOptions[0].SellPrice
	} else {
		goodssItem.Paid = optionModel.SellPrice * int64(goods.GoodsNum)
		goodssItem.SellPrice = optionModel.SellPrice
	}
	return goodssItem, nil
}
